<template>
  <div class="b-back-top" @click="goTop" name="button" v-show="visible" :style="customStyle">
    <svg width="16" height="16" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg" class="back-icon" aria-hidden="true" style="height: 16px; width: 16px;">
      <g>
        <path d="M12.036 15.59c0 .55-.453.995-.997.995H5.032c-.55 0-.997-.445-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29c.39-.39 1.026-.385 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" fill-rule="evenodd"></path>
      </g>
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    visibleHeight: {
      type: Number,
      default: 400
    },
    rate: { // 速率
      type: Number,
      default: 6
    },
    customStyle: {
      type: Object,
      default () {
        return {
          right: '50px',
          bottom: '50px',
          width: '40px',
          height: '40px',
          'border-radius': '4px',
          'line-height': '45px',
          background: '#e7eaf1'
        }
      }
    }
  },
  data () {
    return {
      scrollTop: '',
      visible: false,
      interval: null
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy () {
    window.removeEventListener('scroll', this.handleScroll)
    if (this.interval) {
      clearInterval(this.interval)
    }
  },
  methods: {
    handleScroll () {
      this.visible = window.pageYOffset > this.visibleHeight
      this.scrollTop = window.pageYOffset
    },
    goTop (e) {
      this.interval = setInterval(() => {
        this.scrollTop = this.scrollTop + (-this.scrollTop) / this.rate
        window.scrollTo(0, this.scrollTop)
        if (this.scrollTop <= 0) {
          window.scrollTo(0, 0)
          clearInterval(this.interval)
        }
      }, 16)
    }
  }
}
</script>

<style lang="scss">
.b-back-top{
    position: fixed;
    text-align: center;
    cursor: pointer;
    .back-icon{
      fill: #9aaabf;
      background: none;
    }
}
</style>
